<template>
  <el-container>
      <el-header>
        <h3>教练管理</h3>
      </el-header>
      <el-main>
        <el-form :inline="true" :model="form" ref="form">
          <el-form-item prop="query_field">
            <el-input type="text" suffix-icon="el-icon-search" placeholder="请输入教练名称或者手机号码"  @keyup.enter.native="search" v-model="form.query_field"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click.prevent="search">查询</el-button>
            <el-button type="primary" icon="el-icon-refresh" @click.prevent="reset('form')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-divider></el-divider>
        <el-table
        :data="tableData"
        border
        v-loading="loading"
        >
          <el-table-column label="图像">
            <template slot-scope="scope">
              <el-avatar :src="scope.row.avatar" :size="50"></el-avatar>
            </template>
          </el-table-column>
          <el-table-column
            label="姓名"
            prop="name"
          >
          </el-table-column>
          <el-table-column
            label="手机号"
            prop="mobile"
          ></el-table-column>
          <el-table-column
            label="年龄"
            prop="age"
            sortable
          ></el-table-column>
          <el-table-column
            label="教龄"
            prop="teach_age"
            sortable
          ></el-table-column>
          <el-table-column
            label="属下学员"
          >
            <template slot-scope="scope">
              <el-link v-if="scope.row.student_total" @click="jumpStudent(scope.row.id)">{{scope.row.student_total}}</el-link>
              <el-link v-else>{{scope.row.student_total}}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="招生人数"
          >
            <template slot-scope="scope">
              <el-link v-if="scope.row.enrollment_total" @click="jumpEnrollment(scope.row.id)">{{scope.row.enrollment_total}}</el-link>
              <el-link v-else>{{scope.row.enrollment_total}}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="满意率"
          >
            <template slot-scope="scope">
              <el-link @click="jumpEvaluate(scope.row.id)"> {{scope.row.good_rating}}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="及格率"
          >
            <template slot-scope="scope">
              <el-link>{{scope.row.pass_rating}}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="约车权限"
          >
            <template slot-scope="scope">
              <el-switch v-model="scope.row.auth_status" @change="changeAuth(scope.row.id,scope.$index)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="教学记录"
          >
            <template slot-scope="scope">
              <el-link @click="jumpTeach(scope.row.id)">查看</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
          >
            <template slot-scope="scope">
              <el-button type="primary" size="small" icon="el-icon-view" @click.prevent="info(scope.row.id)">查看</el-button>
              <el-button type="danger" size="small" icon="el-icon-delete" @click.prevent="del(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer>
          <el-pagination
            background
            layout="prev, pager, next, total"
            :total="total"
            :current-page="page"
            :page-size="pageSize"
            @current-change="changePage"
          ></el-pagination>
      </el-footer>
  </el-container>
</template>

<script>
  import {coachList, changeAuth, deleteCoach} from '@/api/coach'
    export default {
        name: "list",
        inject: ['reload'],
        data () {
            return {
                form: {
                    query_field: ''
                },
                tableData: [],
                page: 1,
                total: 0,
                pageSize: 10,
                loading: true
            };
        },
        methods: {
            reset(ref) {
                this.$refs[ref].resetFields();
            },
            search() {
                var params = {};
                if (this.form.query_field.trim().length != 0) {
                    params.query_field = this.form.query_field;
                }
                this.loading = true;
                coachList(params).then(response => {
                    this.tableData = response.data.list;
                    this.page = Number(response.data.paginate.current);
                    this.pageSize = Number(response.data.paginate.pageSize);
                    this.total = Number(response.data.paginate.total);
                    this.loading = false;
                }).catch( err => {
                    this.loading = false;
                })
            },
            changePage (currentPage) {
              var params = {
                  page: currentPage,
                  pageSize: this.pageSize
              };
              if (this.form.query_field.trim().length != 0) {
                  params.query_field = this.form.query_field;
              }
              this.loading = true;
              coachList(params).then(response => {
                  this.tableData = response.data.list;
                  this.page = Number(response.data.paginate.current);
                  this.pageSize = Number(response.data.paginate.pageSize);
                  this.total = Number(response.data.paginate.total);
                  this.loading = false;
              }).catch( err => {
                  this.loading = false;
              })
            },
            jumpStudent(coach_id) {
                this.$router.push({path: '/coach/studentList', query: {coach_id: coach_id}});
            },
            jumpEnrollment(coach_id) {
               this.$router.push({path: '/coach/enrollmentList',query: {coach_id: coach_id}});
            },
            changeAuth(coach_id, index) {
                var status = this.tableData[index].auth_status;
                var coach_id = coach_id;
                var data = {
                    coach_id: coach_id,
                    status: status === true ?  1 : 2,
                };
                changeAuth(data).then(response => {
                    this.$message.success(response.msg);
                    this.reload();
                }).catch( err => {

                });
            },
            jumpTeach(coach_id) {
               this.$router.push({path: '/coach/teach', query: {coach_id: coach_id}});
            },
            jumpEvaluate(coach_id) {
               this.$router.push({path: '/coach/evaluateList', query:{coach_id: coach_id}});
            },
            info(coach_id) {
               this.$router.push({path: '/coach/info', query: {coach_id: coach_id}});
            },
            del(coach_id) {
                this.$confirm('此操作会永久删除该教练信息,是否继续?','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }).then( () => {
                    //如果点击了确定 则执行的逻辑
                    deleteCoach({coach_id: coach_id}).then(response => {
                        this.$message.success(response.msg);
                        this.reload();
                    }).catch( err => {

                    })
                })
            }
        },
        mounted() {
            coachList({}).then(response => {
                this.tableData = response.data.list;
                this.loading = false;
                this.page = response.data.paginate.current;
                this.pageSize = response.data.paginate.pageSize;
                this.total = response.data.paginate.total;
            }).catch( err => {
                console.log(err);
            })
        }
    }
</script>

<style scoped>
  .el-container {
    width: 99%;
    max-width: 100%;
    overflow: hidden;
    border: 1px solid #F2F6FC;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
    margin: 10px auto;
  }
  .el-header {
    text-align: center;
  }
  .el-header h3 {
    font-size: 20px;
    line-height: 20px;
    height: 20px;
  }

  .el-footer{
    text-align: center;
  }
</style>
